import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/train_plan/get_education_plan_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///培训计划(ing)
class TrainIngProgramController extends GetxController {
  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索条件
  String? searchX = '';

  /// 培训途径
  String trainingChannelsId = '';
  String trainingChannelsBack = '请选择';

  /// 培训级别
  String trainingLevelId = '';
  String trainingLevelBack = '请选择';

  /// 培训类型
  String trainingTypeId = '';
  String trainingTypeBack = '请选择';

  /// 开始时间
  String startDate = '';

  /// 结束时间
  String endDate = '';
  /// 培训途径 1线上 2线下
   List<GetDictAllEntity> currencyTrainingChannels = [];
  @override
  void onInit() {
    super.onInit();
    //培训途径 1线上 2线下
    initCurrencyTrainingChannels();
  }

  /// 培训计划列表
  Future<List<GetEducationPlanEntity>> getEducationPlan(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<GetEducationPlanEntity>>(
      Interface.getEducationPlan,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'planName': searchX, //培训名字 模糊查询
        'trainingType': trainingTypeId, //培训类型
        'trainingChannels': trainingChannelsId, //培训途径
        'trainingLevel': trainingLevelId, //培训级别
        // 'status': 3, //调研状态 	surveyStatus状态 0：待提交 1：进行中 2：已结束 //0：待发起 1：待审批 2：审批中 3：通过 4：驳回
        'startDate': startDate, //	开始时间
        'endDate': endDate, //	结束时间
        'type': 1, //	是否待我填写的 1：是
        'isApprovalPage': 1, //是否查询审批数据 1是 0否
      },
    ).execute();
    List<GetEducationPlanEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  ///筛选按钮
  void onFilterButtonPressed() {
    riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 重置
  void reset() {
    // 培训途径
    trainingChannelsId = '';
    trainingChannelsBack = '请选择';

    // 培训级别
    trainingLevelId = '';
    trainingLevelBack = '请选择';

    // 培训类型
    trainingTypeId = '';
    trainingTypeBack = '请选择';
    startDate = '';
    endDate = '';
    globalKey.currentState!.onRefresh();
    update();
  }

  /// 确定
  void confirm() {
    riskRightScaffoldKey.currentState?.closeEndDrawer();
    globalKey.currentState!.onRefresh();
    update();
  }

  GetDictAllEntity? dictEntity1;
  GetDictAllEntity? dictEntity2;
  GetDictAllEntity? dictEntity3;

  ///培训途径 1线上 2线下
  void selectCurrencyTrainingChannels() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '培训途径',
      dataList: currencyTrainingChannels,
      checkedList: [dictEntity1 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity1 = value[0];
        trainingChannelsId = value[0].getId();
        trainingChannelsBack = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  ///培训级别
  void selectPxfsVSWR8mRxType() async{
    final List<GetDictAllEntity> dictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'pxfs_vSWR8mRx');
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '培训级别',
      dataList: dictList,
      checkedList: [dictEntity2 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity2 = value[0];
        trainingLevelId = value[0].getId();
        trainingLevelBack = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  ///培训类型
  void selectPxlxJy31TrQXType() async {
    final List<GetDictAllEntity> dictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'pxlx_Jy31TrQX');
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '培训类型',
      dataList: dictList,
      checkedList: [dictEntity3 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity3 = value[0];
        trainingTypeId = value[0].getId();
        trainingTypeBack = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  ///详情（在线和离线的详情） 培训途径 1:线上 2:线下
  void onClickButtonPressed(GetEducationPlanEntity item) {
    if (item.trainingChannels == '1') {
      Get.toNamed(AppRoutes.onlineTrainingPage, arguments: {'id': item.id, 'isApprove': false, 'isMeTask': true}); //传入计划id 是不是审批 任务进入
    } else {
      Get.toNamed(AppRoutes.offLineTrainingPage, arguments: {'id': item.id, 'isApprove': false, 'isMeTask': true}); //传入计划id 是不是审批 任务进入
    }
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  ///进入课程
  void inputClickPage(GetEducationPlanEntity item) {
    Get.toNamed(AppRoutes.onlineIngCoursePage, arguments: item.id); //传入计划id
  }

  ///审批（在线和离线审批） 培训途径 1:线上 2:线下
  void onClickApprove(GetEducationPlanEntity item) {
    if (item.trainingChannels == '1') {
      Get.toNamed(AppRoutes.onlineTrainingPage, arguments: {'id': item.id, 'isApprove': true, 'isMeTask': true}); //传入计划id 是不是审批 任务进入
    } else {
      Get.toNamed(AppRoutes.offLineTrainingPage, arguments: {'id': item.id, 'isApprove': true, 'isMeTask': true}); //传入计划id 是不是审批 任务进入
    }
  }

  void initCurrencyTrainingChannels() {
    currencyTrainingChannels.clear();
    final GetDictAllEntity getDictAllEntity4 = GetDictAllEntity();
    getDictAllEntity4.id = '1';
    getDictAllEntity4.dictionaryLabel = '线上';
    currencyTrainingChannels.add(getDictAllEntity4);

    final GetDictAllEntity getDictAllEntity5 = GetDictAllEntity();
    getDictAllEntity5.id = '2';
    getDictAllEntity5.dictionaryLabel = '线下';
    currencyTrainingChannels.add(getDictAllEntity5);
  }
}
